package com.liu.lab06.servlet;

import com.liu.lab06.pojo.Music;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import com.liu.lab06.utils.MybatsUtils;
import java.io.IOException;

// 刘小文  -2420710103---
@WebServlet("/addMusic")
public class AddMusicServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1 收集信息
        request.setCharacterEncoding("utf-8");
        String musicname=request.getParameter("musicname");
        String singer=request.getParameter("singer");
        int ranking=Integer.parseInt(request.getParameter("ranking"));
        int category_id = Integer.parseInt(request.getParameter("category_id"));

        Music music = new Music();
        music.setMusicname(musicname);
        music.setSinger(singer);
        music.setRanking(ranking);
        music.setCategory_id(category_id);

        //2 使用Mybatis将音乐信息添加到数据表中
        MybatsUtils.insert("MusicMapper.add", music);

        System.out.println(music.getCategory_id());
        //3 也买你跳转到ShowAllMusicsServlet
        response.sendRedirect(request.getContextPath()+"/showAllMusics");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
